{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "!pip install dspy-ai google-generativeai\n",
        "\n",
        "import dspy\n",
        "import google.generativeai as genai\n",
        "import random\n",
        "from typing import List, Optional\n",
        "\n",
        "GOOGLE_API_KEY = \"Use Your Own API Key\"\n",
        "genai.configure(api_key=GOOGLE_API_KEY)\n",
        "\n",
        "dspy.configure(lm=dspy.LM(model=\"gemini/gemini-1.5-flash\", api_key=GOOGLE_API_KEY))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GyJ8Kh5kMQIO",
        "outputId": "2c0e2301-462d-4bab-bacd-d30ae019d9bc"
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Requirement already satisfied: dspy-ai in /usr/local/lib/python3.11/dist-packages (2.6.27)\n",
            "Requirement already satisfied: google-generativeai in /usr/local/lib/python3.11/dist-packages (0.8.5)\n",
            "Requirement already satisfied: dspy>=2.6.5 in /usr/local/lib/python3.11/dist-packages (from dspy-ai) (2.6.27)\n",
            "Requirement already satisfied: google-ai-generativelanguage==0.6.15 in /usr/local/lib/python3.11/dist-packages (from google-generativeai) (0.6.15)\n",
            "Requirement already satisfied: google-api-core in /usr/local/lib/python3.11/dist-packages (from google-generativeai) (2.25.1)\n",
            "Requirement already satisfied: google-api-python-client in /usr/local/lib/python3.11/dist-packages (from google-generativeai) (2.174.0)\n",
            "Requirement already satisfied: google-auth>=2.15.0 in /usr/local/lib/python3.11/dist-packages (from google-generativeai) (2.38.0)\n",
            "Requirement already satisfied: protobuf in /usr/local/lib/python3.11/dist-packages (from google-generativeai) (5.29.5)\n",
            "Requirement already satisfied: pydantic in /usr/local/lib/python3.11/dist-packages (from google-generativeai) (2.11.7)\n",
            "Requirement already satisfied: tqdm in /usr/local/lib/python3.11/dist-packages (from google-generativeai) (4.67.1)\n",
            "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.11/dist-packages (from google-generativeai) (4.14.0)\n",
            "Requirement already satisfied: proto-plus<2.0.0dev,>=1.22.3 in /usr/local/lib/python3.11/dist-packages (from google-ai-generativelanguage==0.6.15->google-generativeai) (1.26.1)\n",
            "Requirement already satisfied: backoff>=2.2 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (2.2.1)\n",
            "Requirement already satisfied: joblib~=1.3 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (1.5.1)\n",
            "Requirement already satisfied: openai>=0.28.1 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (1.93.0)\n",
            "Requirement already satisfied: pandas>=2.1.1 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (2.2.2)\n",
            "Requirement already satisfied: regex>=2023.10.3 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (2024.11.6)\n",
            "Requirement already satisfied: ujson>=5.8.0 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (5.10.0)\n",
            "Requirement already satisfied: datasets>=2.14.6 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (3.6.0)\n",
            "Requirement already satisfied: requests>=2.31.0 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (2.32.3)\n",
            "Requirement already satisfied: optuna>=3.4.0 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (4.4.0)\n",
            "Requirement already satisfied: magicattr>=0.1.6 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (0.1.6)\n",
            "Requirement already satisfied: litellm>=1.60.3 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (1.73.6)\n",
            "Requirement already satisfied: diskcache>=5.6.0 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (5.6.3)\n",
            "Requirement already satisfied: json-repair>=0.30.0 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (0.47.6)\n",
            "Requirement already satisfied: tenacity>=8.2.3 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (8.5.0)\n",
            "Requirement already satisfied: anyio in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (4.9.0)\n",
            "Requirement already satisfied: asyncer==0.0.8 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (0.0.8)\n",
            "Requirement already satisfied: cachetools>=5.5.0 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (5.5.2)\n",
            "Requirement already satisfied: cloudpickle>=3.0.0 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (3.1.1)\n",
            "Requirement already satisfied: rich>=13.7.1 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (13.9.4)\n",
            "Requirement already satisfied: numpy>=1.26.0 in /usr/local/lib/python3.11/dist-packages (from dspy>=2.6.5->dspy-ai) (2.0.2)\n",
            "Requirement already satisfied: googleapis-common-protos<2.0.0,>=1.56.2 in /usr/local/lib/python3.11/dist-packages (from google-api-core->google-generativeai) (1.70.0)\n",
            "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.11/dist-packages (from google-auth>=2.15.0->google-generativeai) (0.4.2)\n",
            "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.11/dist-packages (from google-auth>=2.15.0->google-generativeai) (4.9.1)\n",
            "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic->google-generativeai) (0.7.0)\n",
            "Requirement already satisfied: pydantic-core==2.33.2 in /usr/local/lib/python3.11/dist-packages (from pydantic->google-generativeai) (2.33.2)\n",
            "Requirement already satisfied: typing-inspection>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from pydantic->google-generativeai) (0.4.1)\n",
            "Requirement already satisfied: httplib2<1.0.0,>=0.19.0 in /usr/local/lib/python3.11/dist-packages (from google-api-python-client->google-generativeai) (0.22.0)\n",
            "Requirement already satisfied: google-auth-httplib2<1.0.0,>=0.2.0 in /usr/local/lib/python3.11/dist-packages (from google-api-python-client->google-generativeai) (0.2.0)\n",
            "Requirement already satisfied: uritemplate<5,>=3.0.1 in /usr/local/lib/python3.11/dist-packages (from google-api-python-client->google-generativeai) (4.2.0)\n",
            "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/dist-packages (from anyio->dspy>=2.6.5->dspy-ai) (3.10)\n",
            "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.11/dist-packages (from anyio->dspy>=2.6.5->dspy-ai) (1.3.1)\n",
            "Requirement already satisfied: filelock in /usr/local/lib/python3.11/dist-packages (from datasets>=2.14.6->dspy>=2.6.5->dspy-ai) (3.18.0)\n",
            "Requirement already satisfied: pyarrow>=15.0.0 in /usr/local/lib/python3.11/dist-packages (from datasets>=2.14.6->dspy>=2.6.5->dspy-ai) (18.1.0)\n",
            "Requirement already satisfied: dill<0.3.9,>=0.3.0 in /usr/local/lib/python3.11/dist-packages (from datasets>=2.14.6->dspy>=2.6.5->dspy-ai) (0.3.7)\n",
            "Requirement already satisfied: xxhash in /usr/local/lib/python3.11/dist-packages (from datasets>=2.14.6->dspy>=2.6.5->dspy-ai) (3.5.0)\n",
            "Requirement already satisfied: multiprocess<0.70.17 in /usr/local/lib/python3.11/dist-packages (from datasets>=2.14.6->dspy>=2.6.5->dspy-ai) (0.70.15)\n",
            "Requirement already satisfied: fsspec<=2025.3.0,>=2023.1.0 in /usr/local/lib/python3.11/dist-packages (from fsspec[http]<=2025.3.0,>=2023.1.0->datasets>=2.14.6->dspy>=2.6.5->dspy-ai) (2025.3.0)\n",
            "Requirement already satisfied: huggingface-hub>=0.24.0 in /usr/local/lib/python3.11/dist-packages (from datasets>=2.14.6->dspy>=2.6.5->dspy-ai) (0.33.1)\n",
            "Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from datasets>=2.14.6->dspy>=2.6.5->dspy-ai) (24.2)\n",
            "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.11/dist-packages (from datasets>=2.14.6->dspy>=2.6.5->dspy-ai) (6.0.2)\n",
            "Requirement already satisfied: grpcio<2.0.0,>=1.33.2 in /usr/local/lib/python3.11/dist-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-ai-generativelanguage==0.6.15->google-generativeai) (1.73.1)\n",
            "Requirement already satisfied: grpcio-status<2.0.0,>=1.33.2 in /usr/local/lib/python3.11/dist-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0dev,>=1.34.1->google-ai-generativelanguage==0.6.15->google-generativeai) (1.71.2)\n",
            "Requirement already satisfied: pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2 in /usr/local/lib/python3.11/dist-packages (from httplib2<1.0.0,>=0.19.0->google-api-python-client->google-generativeai) (3.2.3)\n",
            "Requirement already satisfied: aiohttp>=3.10 in /usr/local/lib/python3.11/dist-packages (from litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (3.11.15)\n",
            "Requirement already satisfied: click in /usr/local/lib/python3.11/dist-packages (from litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (8.2.1)\n",
            "Requirement already satisfied: httpx>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (0.28.1)\n",
            "Requirement already satisfied: importlib-metadata>=6.8.0 in /usr/local/lib/python3.11/dist-packages (from litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (8.7.0)\n",
            "Requirement already satisfied: jinja2<4.0.0,>=3.1.2 in /usr/local/lib/python3.11/dist-packages (from litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (3.1.6)\n",
            "Requirement already satisfied: jsonschema<5.0.0,>=4.22.0 in /usr/local/lib/python3.11/dist-packages (from litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (4.24.0)\n",
            "Requirement already satisfied: python-dotenv>=0.2.0 in /usr/local/lib/python3.11/dist-packages (from litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (1.1.1)\n",
            "Requirement already satisfied: tiktoken>=0.7.0 in /usr/local/lib/python3.11/dist-packages (from litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (0.9.0)\n",
            "Requirement already satisfied: tokenizers in /usr/local/lib/python3.11/dist-packages (from litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (0.21.2)\n",
            "Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/dist-packages (from openai>=0.28.1->dspy>=2.6.5->dspy-ai) (1.9.0)\n",
            "Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from openai>=0.28.1->dspy>=2.6.5->dspy-ai) (0.10.0)\n",
            "Requirement already satisfied: alembic>=1.5.0 in /usr/local/lib/python3.11/dist-packages (from optuna>=3.4.0->dspy>=2.6.5->dspy-ai) (1.16.2)\n",
            "Requirement already satisfied: colorlog in /usr/local/lib/python3.11/dist-packages (from optuna>=3.4.0->dspy>=2.6.5->dspy-ai) (6.9.0)\n",
            "Requirement already satisfied: sqlalchemy>=1.4.2 in /usr/local/lib/python3.11/dist-packages (from optuna>=3.4.0->dspy>=2.6.5->dspy-ai) (2.0.41)\n",
            "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.11/dist-packages (from pandas>=2.1.1->dspy>=2.6.5->dspy-ai) (2.9.0.post0)\n",
            "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.11/dist-packages (from pandas>=2.1.1->dspy>=2.6.5->dspy-ai) (2025.2)\n",
            "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.11/dist-packages (from pandas>=2.1.1->dspy>=2.6.5->dspy-ai) (2025.2)\n",
            "Requirement already satisfied: pyasn1<0.7.0,>=0.6.1 in /usr/local/lib/python3.11/dist-packages (from pyasn1-modules>=0.2.1->google-auth>=2.15.0->google-generativeai) (0.6.1)\n",
            "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests>=2.31.0->dspy>=2.6.5->dspy-ai) (3.4.2)\n",
            "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests>=2.31.0->dspy>=2.6.5->dspy-ai) (2.4.0)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests>=2.31.0->dspy>=2.6.5->dspy-ai) (2025.6.15)\n",
            "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.11/dist-packages (from rich>=13.7.1->dspy>=2.6.5->dspy-ai) (3.0.0)\n",
            "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.11/dist-packages (from rich>=13.7.1->dspy>=2.6.5->dspy-ai) (2.19.2)\n",
            "Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp>=3.10->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (2.6.1)\n",
            "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.11/dist-packages (from aiohttp>=3.10->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (1.3.2)\n",
            "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp>=3.10->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (25.3.0)\n",
            "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.11/dist-packages (from aiohttp>=3.10->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (1.7.0)\n",
            "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.11/dist-packages (from aiohttp>=3.10->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (6.6.3)\n",
            "Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp>=3.10->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (0.3.2)\n",
            "Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp>=3.10->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (1.20.1)\n",
            "Requirement already satisfied: Mako in /usr/lib/python3/dist-packages (from alembic>=1.5.0->optuna>=3.4.0->dspy>=2.6.5->dspy-ai) (1.1.3)\n",
            "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx>=0.23.0->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (1.0.9)\n",
            "Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx>=0.23.0->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (0.16.0)\n",
            "Requirement already satisfied: hf-xet<2.0.0,>=1.1.2 in /usr/local/lib/python3.11/dist-packages (from huggingface-hub>=0.24.0->datasets>=2.14.6->dspy>=2.6.5->dspy-ai) (1.1.5)\n",
            "Requirement already satisfied: zipp>=3.20 in /usr/local/lib/python3.11/dist-packages (from importlib-metadata>=6.8.0->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (3.23.0)\n",
            "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.11/dist-packages (from jinja2<4.0.0,>=3.1.2->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (3.0.2)\n",
            "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.11/dist-packages (from jsonschema<5.0.0,>=4.22.0->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (2025.4.1)\n",
            "Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.11/dist-packages (from jsonschema<5.0.0,>=4.22.0->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (0.36.2)\n",
            "Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.11/dist-packages (from jsonschema<5.0.0,>=4.22.0->litellm>=1.60.3->dspy>=2.6.5->dspy-ai) (0.26.0)\n",
            "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.11/dist-packages (from markdown-it-py>=2.2.0->rich>=13.7.1->dspy>=2.6.5->dspy-ai) (0.1.2)\n",
            "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.11/dist-packages (from python-dateutil>=2.8.2->pandas>=2.1.1->dspy>=2.6.5->dspy-ai) (1.17.0)\n",
            "Requirement already satisfied: greenlet>=1 in /usr/local/lib/python3.11/dist-packages (from sqlalchemy>=1.4.2->optuna>=3.4.0->dspy>=2.6.5->dspy-ai) (3.2.3)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "class QuestionAnswering(dspy.Signature):\n",
        "    \"\"\"Answer questions based on given context with reasoning.\"\"\"\n",
        "    context: str = dspy.InputField(desc=\"Relevant context information\")\n",
        "    question: str = dspy.InputField(desc=\"Question to answer\")\n",
        "    reasoning: str = dspy.OutputField(desc=\"Step-by-step reasoning\")\n",
        "    answer: str = dspy.OutputField(desc=\"Final answer\")\n",
        "\n",
        "class FactualityCheck(dspy.Signature):\n",
        "    \"\"\"Verify if an answer is factually correct given context.\"\"\"\n",
        "    context: str = dspy.InputField()\n",
        "    question: str = dspy.InputField()\n",
        "    answer: str = dspy.InputField()\n",
        "    is_correct: bool = dspy.OutputField(desc=\"True if answer is factually correct\")"
      ],
      "metadata": {
        "id": "Mymfj8M2MhIa"
      },
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "class AdvancedQA(dspy.Module):\n",
        "    def __init__(self, max_retries: int = 2):\n",
        "        super().__init__()\n",
        "        self.max_retries = max_retries\n",
        "        self.qa_predictor = dspy.ChainOfThought(QuestionAnswering)\n",
        "        self.fact_checker = dspy.Predict(FactualityCheck)\n",
        "\n",
        "    def forward(self, context: str, question: str) -> dspy.Prediction:\n",
        "        prediction = self.qa_predictor(context=context, question=question)\n",
        "\n",
        "        for attempt in range(self.max_retries):\n",
        "            fact_check = self.fact_checker(\n",
        "                context=context,\n",
        "                question=question,\n",
        "                answer=prediction.answer\n",
        "            )\n",
        "\n",
        "            if fact_check.is_correct:\n",
        "                break\n",
        "\n",
        "            refined_context = f\"{context}\\n\\nPrevious incorrect answer: {prediction.answer}\\nPlease provide a more accurate answer.\"\n",
        "            prediction = self.qa_predictor(context=refined_context, question=question)\n",
        "\n",
        "        return prediction"
      ],
      "metadata": {
        "id": "Xpo2YohnMsut"
      },
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "class SimpleRAG(dspy.Module):\n",
        "    def __init__(self, knowledge_base: List[str]):\n",
        "        super().__init__()\n",
        "        self.knowledge_base = knowledge_base\n",
        "        self.qa_system = AdvancedQA()\n",
        "\n",
        "    def retrieve(self, question: str, top_k: int = 2) -> str:\n",
        "        scored_docs = []\n",
        "        question_words = set(question.lower().split())\n",
        "\n",
        "        for doc in self.knowledge_base:\n",
        "            doc_words = set(doc.lower().split())\n",
        "            score = len(question_words.intersection(doc_words))\n",
        "            scored_docs.append((score, doc))\n",
        "\n",
        "        scored_docs.sort(reverse=True)\n",
        "        return \"\\n\\n\".join([doc for _, doc in scored_docs[:top_k]])\n",
        "\n",
        "    def forward(self, question: str) -> dspy.Prediction:\n",
        "        context = self.retrieve(question)\n",
        "        return self.qa_system(context=context, question=question)"
      ],
      "metadata": {
        "id": "EfwN-10bM0y6"
      },
      "execution_count": 7,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "knowledge_base = [\n",
        "    \"The Eiffel Tower is a wrought-iron lattice tower located in Paris, France. It was constructed from 1887 to 1889 and stands 330 meters tall including antennas.\",\n",
        "    \"Python is a high-level programming language created by Guido van Rossum. It was first released in 1991 and emphasizes code readability.\",\n",
        "    \"Machine learning is a subset of artificial intelligence that focuses on algorithms that can learn from data without being explicitly programmed.\",\n",
        "    \"The Great Wall of China is a series of fortifications built across northern China. Construction began in the 7th century BC and continued for centuries.\",\n",
        "    \"Photosynthesis is the process by which plants convert light energy into chemical energy, typically using chlorophyll to absorb sunlight.\"\n",
        "]\n",
        "\n",
        "training_examples = [\n",
        "    dspy.Example(\n",
        "        question=\"What is the height of the Eiffel Tower?\",\n",
        "        context=\"The Eiffel Tower is a wrought-iron lattice tower located in Paris, France. It was constructed from 1887 to 1889 and stands 330 meters tall including antennas.\",\n",
        "        answer=\"330 meters\"\n",
        "    ).with_inputs(\"question\", \"context\"),\n",
        "\n",
        "    dspy.Example(\n",
        "        question=\"Who created Python programming language?\",\n",
        "        context=\"Python is a high-level programming language created by Guido van Rossum. It was first released in 1991 and emphasizes code readability.\",\n",
        "        answer=\"Guido van Rossum\"\n",
        "    ).with_inputs(\"question\", \"context\"),\n",
        "\n",
        "    dspy.Example(\n",
        "        question=\"What is machine learning?\",\n",
        "        context=\"Machine learning is a subset of artificial intelligence that focuses on algorithms that can learn from data without being explicitly programmed.\",\n",
        "        answer=\"Machine learning is a subset of AI that focuses on algorithms that learn from data without explicit programming.\"\n",
        "    ).with_inputs(\"question\", \"context\")\n",
        "]"
      ],
      "metadata": {
        "id": "ackTOHnNM9CZ"
      },
      "execution_count": 8,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def accuracy_metric(example, prediction, trace=None):\n",
        "    \"\"\"Simple accuracy metric for evaluation\"\"\"\n",
        "    return example.answer.lower() in prediction.answer.lower()\n",
        "\n",
        "print(\"🚀 Initializing DSPy QA System with Gemini...\")\n",
        "print(\"📝 Note: Using Google's Gemini 1.5 Flash (free tier)\")\n",
        "rag_system = SimpleRAG(knowledge_base)\n",
        "\n",
        "basic_qa = dspy.ChainOfThought(QuestionAnswering)\n",
        "\n",
        "print(\"\\n📊 Before Optimization:\")\n",
        "test_question = \"What is the height of the Eiffel Tower?\"\n",
        "test_context = knowledge_base[0]\n",
        "initial_prediction = basic_qa(context=test_context, question=test_question)\n",
        "print(f\"Q: {test_question}\")\n",
        "print(f\"A: {initial_prediction.answer}\")\n",
        "print(f\"Reasoning: {initial_prediction.reasoning}\")\n",
        "\n",
        "print(\"\\n🔧 Optimizing with BootstrapFewShot...\")\n",
        "optimizer = dspy.BootstrapFewShot(metric=accuracy_metric, max_bootstrapped_demos=2)\n",
        "optimized_qa = optimizer.compile(basic_qa, trainset=training_examples)\n",
        "\n",
        "print(\"\\n📈 After Optimization:\")\n",
        "optimized_prediction = optimized_qa(context=test_context, question=test_question)\n",
        "print(f\"Q: {test_question}\")\n",
        "print(f\"A: {optimized_prediction.answer}\")\n",
        "print(f\"Reasoning: {optimized_prediction.reasoning}\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AhBeOjGINEO1",
        "outputId": "883aa1bd-286f-42bc-87b3-a0a10c947313"
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "🚀 Initializing DSPy QA System with Gemini...\n",
            "📝 Note: Using Google's Gemini 1.5 Flash (free tier)\n",
            "\n",
            "📊 Before Optimization:\n",
            "Q: What is the height of the Eiffel Tower?\n",
            "A: 330 meters\n",
            "Reasoning: The provided text states that the Eiffel Tower stands 330 meters tall, including antennas.  Therefore, the answer to the question is 330 meters.\n",
            "\n",
            "🔧 Optimizing with BootstrapFewShot...\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            " 67%|██████▋   | 2/3 [00:00<00:00, 284.48it/s]"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Bootstrapped 2 full traces after 2 examples for up to 1 rounds, amounting to 2 attempts.\n",
            "\n",
            "📈 After Optimization:\n",
            "Q: What is the height of the Eiffel Tower?\n",
            "A: 330 meters\n",
            "Reasoning: The passage explicitly states that the Eiffel Tower stands 330 meters tall, including antennas.\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(\"\\n🎯 Advanced RAG Demo:\")\n",
        "test_questions = [\n",
        "    \"What is photosynthesis?\",\n",
        "    \"When was Python first released?\",\n",
        "    \"Where is the Great Wall of China located?\"\n",
        "]\n",
        "\n",
        "for question in test_questions:\n",
        "    print(f\"\\n❓ Question: {question}\")\n",
        "    result = rag_system(question=question)\n",
        "    print(f\"💡 Answer: {result.answer}\")\n",
        "    print(f\"🧠 Reasoning: {result.reasoning[:100]}...\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IwDfMK8RNQGt",
        "outputId": "44397ff4-7ad2-44e4-e396-c4e7cd5d5283"
      },
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "🎯 Advanced RAG Demo:\n",
            "\n",
            "❓ Question: What is photosynthesis?\n",
            "💡 Answer: I cannot answer this question based on the provided text.\n",
            "🧠 Reasoning: The provided text describes the Great Wall of China and the Eiffel Tower.  It does not contain any i...\n",
            "\n",
            "❓ Question: When was Python first released?\n",
            "💡 Answer: 1991\n",
            "🧠 Reasoning: The provided text states that \"Python is a high-level programming language created by Guido van Ross...\n",
            "\n",
            "❓ Question: Where is the Great Wall of China located?\n",
            "💡 Answer: Northern China\n",
            "🧠 Reasoning: The provided text states that the Great Wall of China is \"a series of fortifications built across no...\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "j1_ua6GwCZn_",
        "outputId": "a8eb3736-ef7e-483e-cc79-27a02fe888e8"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "📊 Evaluation Results:\n",
            "Basic QA Accuracy: 66.67%\n",
            "Optimized QA Accuracy: 66.67%\n",
            "\n",
            "✅ Tutorial Complete! Key DSPy Concepts Demonstrated:\n",
            "1. 🔤 Signatures - Defined input/output schemas\n",
            "2. 🏗️  Modules - Built composable QA systems\n",
            "3. 🔄 Self-correction - Implemented iterative improvement\n",
            "4. 🔍 RAG - Created retrieval-augmented generation\n",
            "5. ⚡ Optimization - Used BootstrapFewShot to improve prompts\n",
            "6. 📊 Evaluation - Measured system performance\n",
            "7. 🆓 Free API - Powered by Google Gemini 1.5 Flash\n"
          ]
        }
      ],
      "source": [
        "def evaluate_system(qa_module, test_cases):\n",
        "    \"\"\"Evaluate QA system performance\"\"\"\n",
        "    correct = 0\n",
        "    total = len(test_cases)\n",
        "\n",
        "    for example in test_cases:\n",
        "        prediction = qa_module(context=example.context, question=example.question)\n",
        "        if accuracy_metric(example, prediction):\n",
        "            correct += 1\n",
        "\n",
        "    return correct / total\n",
        "\n",
        "print(f\"\\n📊 Evaluation Results:\")\n",
        "print(f\"Basic QA Accuracy: {evaluate_system(basic_qa, training_examples):.2%}\")\n",
        "print(f\"Optimized QA Accuracy: {evaluate_system(optimized_qa, training_examples):.2%}\")\n",
        "\n",
        "print(\"\\n✅ Tutorial Complete! Key DSPy Concepts Demonstrated:\")\n",
        "print(\"1. 🔤 Signatures - Defined input/output schemas\")\n",
        "print(\"2. 🏗️  Modules - Built composable QA systems\")\n",
        "print(\"3. 🔄 Self-correction - Implemented iterative improvement\")\n",
        "print(\"4. 🔍 RAG - Created retrieval-augmented generation\")\n",
        "print(\"5. ⚡ Optimization - Used BootstrapFewShot to improve prompts\")\n",
        "print(\"6. 📊 Evaluation - Measured system performance\")\n",
        "print(\"7. 🆓 Free API - Powered by Google Gemini 1.5 Flash\")"
      ]
    }
  ]
}